package cc3002;

/**
 * Weird Food with weird effects. Some are predictable, some are random. You can receive more
 * calories but with a cost... The idea is to use extended classes, but if you want, you can
 * use this constructor and the possible costs and benefits are moderated.
 * @author Anibal Llanos Prado
 *
 */
public class WeirdFood extends Food{
	
	protected int bad, good, caloriesPerBite=300, sicknessLimit, badStackLimit, goodStackLimit;
	/**
	 * Weird Food constructor. It works.
	 */
	public WeirdFood(){
		this.sicknessLimit=5;
		this.badStackLimit=1;
		this.goodStackLimit=3;
		this.remainingBites=3;
		this.name="Weird Food";
	}
	
	/**
	 * Secondary constructor, allows you to set a name for the Weird Food.
	 * @param name The name the Weird Food will have.
	 */
	public WeirdFood(String name){
		this.sicknessLimit=5;
		this.badStackLimit=1;
		this.goodStackLimit=3;
		this.remainingBites=3;
		this.name=name;
	}
	/**
	 * Bites the food. This method is overridden to modify it behavior.
	 * @param health The Health State of the biter.
	 */
	@Override
	public int bite(HealthState health){
		assert remainingBites!=0:"Nothing to eat";
		if(remainingBites==0)return 0;
		--remainingBites;
		int aux=(int)(caloriesPerBite*health.getFoodMultiplier());
		modifyHealth(health);
		return aux;
	}
	
	/**
	 * Allows you to modify the Health State. The modification depends on the previous
	 * state and some random parameters.
	 * @param health The Health State we want to modify.
	 */
	protected void modifyHealth(HealthState health){
		int hm = (int)(Math.random()*10);
		getGoodAndBad(health.getFoodSaver());
		if(hm<=sicknessLimit){
			if(bad>=badStackLimit){
				bad=0;
				if(health.getState()!=0)health.getWorst();
			}
			else ++bad;
		}
		else{
			if(good>=goodStackLimit){
				good=0;
				if(health.getState()!=6)health.getBetter();
			}
			else ++good;
		}
		health.setFoodSaver(good, bad);
	}
	
	/**
	 * Extracts the "good" and "bad" parts of the foodSaver (memory) of the Health State.
	 * @param foodSaver The saver (memory) of the Health State.
	 */
	protected void getGoodAndBad(String foodSaver){
		good=Integer.parseInt(foodSaver.substring(0,1));
		bad=Integer.parseInt(foodSaver.substring(1));
	}
	
	/**
	 * Sets the calories the Food will give when somebody bits it. Overrides the parent's method.
	 * @param calories The new amount of calories.
	 */
	@Override
	public void setCalories(int calories) {
		this.caloriesPerBite = calories;
	}

}
